﻿/*******************************************************************************
 **  Name:                                        **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                            **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
// Created on November 1, 2010, 7:25 AM
// Modified $Date$ by $Author$
// $Id$

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace sat1baf1
{
    public class Career
    {
        private string _name;

        public string Name { get { return _name; } set { _name = value; } }
    }

    public sealed class CareerDB
    {
        static string _fileName;
        XElement _x;

        static CareerDB _instance;
        public static CareerDB Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CareerDB();
                return _instance;
            }
        }

        private CareerDB()
        {
            _fileName = @"Data Files\Classes.xml";
            _x = XElement.Load(_fileName); // Root element
            XmlLog.Instance.Add("Loaded CareerDB log from " + _fileName);
        }

        // TODO
        //public Career GetCareer(string name)
        //{
        //    IEnumerable<XElement> org =
        //        from el in _x
        //        where (string)el.Element("Name") == name
        //        select el;

        //    Career newCareer = new Career();

        //    return newCareer;
        //}


        // TODO
        public IEnumerable<XElement> FindCareerByName(string name)
        {
            // The event arg name is the name of the node
#if DEBUG
            XmlLog.Instance.Add("FindCareerByName: Looking for <Name>" + name + "</Name>");

#endif
            IEnumerable<XElement> org =
                from x in _x.Elements("Class")
                where (string)x == name
                select x;

            return org;
        }

        public void Add(XElement newCareer)
        {
            _x.Add(newCareer);
            _x.Save(_fileName);
        }

        public void Clear()
        {
            _x.RemoveNodes();
            _x.Save(_fileName);
        }


        // Properties
        // Read-only
        public XElement Careers { get { return _x; } }
    }



}
